package com.lwl.controller.sds;

import com.github.pagehelper.PageInfo;
import com.lwl.entity.*;
import com.lwl.entity.mht.TQuestionMht;
import com.lwl.entity.sds.TSdsQuestion;
import com.lwl.entity.sds.TSdsRecordDetail;
import com.lwl.service.sds.TSdsQuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (TSdsQuestion)表控制层
 *
 * @author Lei
 * @since 2022-11-09 21:22:59
 */
@Controller
@RequestMapping("tSdsQuestion")
public class TSdsQuestionController {
    /**
     * 服务对象
     */
    @Autowired
    private TSdsQuestionService tSdsQuestionService;


    @GetMapping("queryAll")
    @ResponseBody
    public LayData queryAll(MyPage myPage, TQuestionMht tQuestionMht){
        PageInfo tQuestionMhts = tSdsQuestionService.queryAll(myPage,tQuestionMht);
        return new LayData(tQuestionMhts);
    }

    /**
     * 添加题目
     * @return
     */
    @PostMapping("registerQuestion")
    @ResponseBody
    public LayData registerQuestion(TSdsQuestion tSdsQuestion){
        boolean status = tSdsQuestionService.insertQuestion(tSdsQuestion);
        return new LayData(status);
    }

    /**
     * 通过easyExcel在题目展示页面，进行批量添加题目
     * @param file
     * @return
     */
    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        boolean status =  tSdsQuestionService.dealFile(file);
        return new LayData(status);
    }

    /**
     * 批量删除
     * @param dataId
     * @return
     */
    @PostMapping("deleteMoreQuestion/{dataId}")
    @ResponseBody
    public LayData deleteMoreQuestion(@PathVariable Integer[] dataId){
        System.out.println("dataId = " + dataId);
        List<Integer> status = tSdsQuestionService.deleteMoreQuestion(dataId);
        return new LayData(status);
    }

    /**
     *  删除题目
     * @return
     */
    @PostMapping("deleteQuestion")
    @ResponseBody
    public LayData deleteQuestion(Integer id){
        boolean status = false;
//        先查询题号为id的题目是否已经被使用过，如果使用过则不能进行删除
        List<TSdsRecordDetail> tSdsRecordDetailList = tSdsQuestionService.queryTMhtRecordDetail(id);
        if (tSdsRecordDetailList.size()==0){
            status = tSdsQuestionService.deleteQuestion(id);
        }
        return new LayData(status);
    }

    /**
     * sds做题，查找所有题目
     */
    @GetMapping("allQuestion")
    public String allQuestion(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(20);
        myPage.setPage(1);
        PageInfo all = tSdsQuestionService.getAll(myPage);
        request.setAttribute("allSub",all);
        return "sds/index";
    }

}
